package com.controller;

import com.bean.Commodity;
import com.bean.Provider;
import com.service.CommodityService;
import com.service.ProviderService;
import com.util.PageAssistant;
import com.util.ParamUtil;
import com.vo.ProviderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 界面业务控制器
 */
@Controller
@RequestMapping("provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;
    @Autowired
    private CommodityService commodityService;

    private ParamUtil paramUtil = new ParamUtil();


    @RequestMapping("query")
    public String check(Model model, Provider provider, HttpSession session,
                        HttpServletRequest request, PageAssistant<Provider> paPro){
        System.out.println();
        System.out.println(">>>>   provider query");
        System.out.println("------------------------------------------");

        Map<String,Object> map = new HashMap<>();

        if (provider.getId() != null && !provider.getId().equals("")){
            map.put("id",provider.getId());
        }

        if (provider.getName() != null && !provider.getName().trim().equals("")){
            map.put("name",provider.getName());
        }
        paPro.setCount(providerService.count(map));

        if (paPro.getColumn() == null){
            paPro.setColumn("id");
        }
        if (paPro.getState() == null){
            paPro.setState("asc");
        }
        map.put("start",paPro.getStart());
        map.put("size",paPro.getSize());

        paPro.setItems(providerService.query(map));

        //资源路径
        session.setAttribute("currUrl", "/provider/query");
        //参数字符串
        session.setAttribute("currParam", paramUtil.getParamStr(request));

        model.addAttribute("provider",provider);
        model.addAttribute("paPro",paPro);
        model.addAttribute("providerList",providerService.find(new Provider()));
        model.addAttribute("updateMsg", session.getAttribute("updateMsg"));
        model.addAttribute("updateClass", session.getAttribute("updateClass"));
        model.addAttribute("selectByPage",new String[paPro.getTotal()]);

        session.setAttribute("currUrl","/provider/query");
        session.setAttribute("currParam",paramUtil.getParamStr(request));

        return "/provider/providerList";
    }

    @RequestMapping("view")
    public String view(Model model, Provider provider){
        System.out.println();
        System.out.println(">>>>   provider view");
        System.out.println("------------------------------------------");

        System.out.println(provider);

        List<Provider> providerList = providerService.find(provider);

        System.out.println(providerList.get(0));
        model.addAttribute("providerView",providerList.get(0));

        return "/provider/detail";

    }


    @RequestMapping("updatePage")
    public String updatePage(Model model, Provider provider, HttpServletRequest request, HttpSession session){
        System.out.println();
        System.out.println(">>>>   provider updatePage");
        System.out.println("------------------------------------------");

        if (session.getAttribute("updateMsg") != null){
            model.addAttribute(session.getAttribute("updateMsg"));
            model.addAttribute(session.getAttribute("updateClass"));
            session.removeAttribute("updateMsg");
            session.removeAttribute("updateClass");
        }

        if (session.getAttribute("currUrl") != null &&
                !session.getAttribute("currUrl").equals("/provider/updatePage")){
            session.setAttribute("currUrl", "/provider/updatePage");
            session.setAttribute("currParam", paramUtil.getParamStr(request));
        }

        List<Provider> providerList = providerService.find(provider);

        Map<String,Object> map = new HashMap<>();
        map.put("providerId",providerList.get(0).getId());
        map.put("order","id asc");

        providerList.get(0).setCommodityList(commodityService.query(map));

        model.addAttribute("providerInfo",providerList.get(0));

        return "/provider/providerUpdate";
    }


    @RequestMapping("addPage")
    public String updatePage(){
        System.out.println();
        System.out.println(">>>>   provider addPage");
        System.out.println("------------------------------------------");

        return "/provider/providerAdd";
    }

    @RequestMapping("update")
    public String update(ProviderVo provider, HttpSession session){
        System.out.println(); System.out.println(provider);
        System.out.println(">>>>   provider update");
        System.out.println("------------------------------------------");

        try {
            providerService.modify(provider.back());
            session.setAttribute("updateMsg","修改成功！");
            session.setAttribute("updateClass","update-suc");
        } catch (Exception e) {
            System.out.println(e);
            session.setAttribute("updateMsg","修改失败！");
            session.setAttribute("updateClass","update-err");
        }

        return "redirect:/provider/updatePage?id="+provider.getId();
    }


    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> delete(HttpSession session, Provider provider, Commodity commodity){
        System.out.println();
        System.out.println(">>>>   provider delete");
        System.out.println("------------------------------------------");

        Map<String,Object> map = new HashMap<>();

        if (commodity.getProviderId() != null){
            try {
                commodityService.remove(commodity);
                map.put("msg","删除成功");
            } catch (Exception e) {
                System.out.println(e);
                map.put("msg","删除失败");
            }

            map.put("backUrl","/provider/updatePage");
            map.put("backParam","id="+commodity.getProviderId());
            return map;
        }

        try {
            providerService.remove(provider);
            map.put("msg","删除成功");
            map.put("backUrl","/provider/query");
            map.put("backParam","");
            return map;
        } catch (Exception e) {
            map.put("msg","删除失败");
            map.put("backUrl","/provider/updatePage");
            map.put("backParam","id="+provider.getId());
            return map;
        }
    }


    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(ProviderVo provider , HttpSession session) throws Exception {
        System.out.println();
        System.out.println(">>>>   provider add");
        System.out.println("------------------------------------------");

        System.out.println(provider);

        Map<String,Object> map = new HashMap<>();

        List<Provider> providerList = providerService.find(provider.back());

        map.put("backUrl","/main");
        session.setAttribute("currUrl","/provider/addPage");

        if (providerList.size() > 0){
            map.put("msg","该公司已存在！");
            return map;
        }

        try {
            providerService.save(provider.back());
            map.put("msg","添加成功！快去添加商品吧！");
            session.setAttribute("currUrl","/provider/updatePage");
            session.setAttribute("currParam","name="+provider.getName());
            return map;

        } catch (Exception e) {
            map.put("msg","添加失败");
            return map;
        }

    }

}




















